package com.relife.sys.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.relife.common.model.AjaxJson;
import com.relife.common.model.Page;
import com.relife.core.easyui.Easyui;
import com.relife.core.entity.CronEntity;
import com.relife.core.qbc.QbcQuery;
import com.relife.sys.service.CronService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.relife.common.utils.StringUtil;


/**
 * 定时任务配置
 * 
 * @author WU
 * @date 2017-04-18 15:02:13
 */
@Controller
@RequestMapping("sys/cron")
public class CronController extends BaseController {
	@Autowired
	private CronService cronService;

	@RequestMapping
	public String cronList(HttpServletRequest req) {
		return "sys/cron/cronList";
	}

	/**
	 * 数据列表
	 * 
	 * @author WU
	 * @date 2017-04-18 15:02:13
	 * @param cron
	 * @param page
	 */
	@RequestMapping
	public void listData(CronEntity cron, Page page, HttpServletResponse res) {
		QbcQuery qbc = new QbcQuery(cron);
		cronService.getPage(qbc, page);
		Easyui.datagrid(page, res);
	}

	/**
	 * @author WU
	 * @date 2017-04-18 15:02:13
	 * @param cron
	 * @param req
	 * @return
	 */
	@RequestMapping
	public String cron(CronEntity cron, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(cron.getId())) {
			cron = cronService.get(CronEntity.class, cron.getId());
		}
		req.setAttribute("page", cron);
		return "sys/cron/cron";
	}

	/**
	 * 保存
	 * 
	 * @author WU
	 * @date 2017-04-18 15:02:13
	 * @param cron
	 * @return
	 */
	@ResponseBody
	@RequestMapping
	public AjaxJson save(CronEntity cron, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			cronService.saveOrUpdateCron(cron);
		} catch (Exception e) {
			j.setException(e);
			e.printStackTrace();
		}
		return j;
	}

	/**
	 * @author WU
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping
	public AjaxJson isStart(String id, Integer state) {
		AjaxJson j = new AjaxJson();
		try {
			cronService.isStart(id, state);
		} catch (Exception e) {
			j.setException(e);
			e.printStackTrace();
		}
		return j;
	}

	/**
	 * 删除
	 * 
	 * @author WU
	 * @date 2017-04-18 15:02:13
	 * @param cron
	 * @return
	 */
	@ResponseBody
	@RequestMapping
	public AjaxJson del(CronEntity cron) {
		AjaxJson j = new AjaxJson();
		try {
			cronService.delCron(cron);
		} catch (Exception e) {
			j.setException(e);
			e.printStackTrace();
		}
		return j;
	}
}
